package lol.niconico.libtest.data.db

import androidx.paging.PagingSource
import androidx.room.*
import lol.niconico.libtest.entity.RemoteKeysEntity
import lol.niconico.libtest.entity.UserEntity

/**
 * Created by ZhangQianqian on 2020/7/17  11:16.
 * email 415692689@qq.com
 */
@Database(
    entities = [UserEntity::class, RemoteKeysEntity::class],
    version = 1, exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
    abstract fun remoteKeysDao(): RemoteKeysDao
}

@Dao
interface UserDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertUser(pokemonList: List<UserEntity>)

    @Query("SELECT * FROM UserEntity")
    fun getUser(): PagingSource<Int, UserEntity>


    @Query("DELETE FROM UserEntity")
    suspend fun clearUser()
}

@Dao
interface RemoteKeysDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAll(remoteKeyEntity: RemoteKeysEntity)

    @Query("SELECT * FROM RemoteKeysEntity where remoteName = :name ")
    suspend fun getRemoteKeys(name: String): RemoteKeysEntity?

    @Query("DELETE FROM RemoteKeysEntity where remoteName = :name")
    suspend fun clearRemoteKeys(name: String)
}